package com.auth.web.spring;


import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auth.domain.T_user_role;
import com.auth.service.T_user_roleService;
import com.common.db.JsonVO;
import com.common.db.Pg;
import com.common.utils.BeanUtils;

@Controller
@RequestMapping("/t_user_role")

public class T_user_roleController
{
	private static Logger log = Logger.getLogger(T_user_roleController.class.getName());

	@Resource
    private T_user_roleService service;
	private Pg pg;

	/**
	 * 0、跳转到 toT_userRole信息列表；
	 * @param request
	 * @return
	 */
	@RequestMapping("/toT_userRole")
	public String toT_userRole(){
    	log.debug("------跳转到/WEB-INF/views/auth/t_user_role/t_user_roleSet.jsp------");
        return "auth/t_user_role/t_user_roleSet";
	}

	/**
	 * 1、获取 T_user_role信息列表，并且在列表页面展示
	 * @param request
	 * @return Pg，json格式
	 */
	@RequestMapping("/getAllT_user_role")
	@ResponseBody
	public Pg getAllT_user_role(HttpServletRequest request){
        int pageSize = 10;
        int startIndex = 0;
        T_user_role t_user_role = new T_user_role();
        try{
	        if(request.getParameter("pageSize")!=null){
	        	pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
	        }
	        if(request.getParameter("startIndex")!=null){
	            startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
	        }
	        if(request.getParameter("process_code")!=null){ //取得动作流 流程英文名称，后台查询数据关联使用
	        	t_user_role.setProcess_code(request.getParameter("process_code"));
	        }
			if(request.getParameter("userid")!=null&&!"".equals(request.getParameter("userid"))){
	        	t_user_role.setUserid(request.getParameter("userid"));
	        }
      	    if(request.getParameter("id")!=null&&!"".equals(request.getParameter("id"))){
	        	t_user_role.setId(new Integer(request.getParameter("id")));
	        }
      	    if(request.getParameter("roleid")!=null&&!"".equals(request.getParameter("roleid"))){
	        	t_user_role.setRoleid(new Integer(request.getParameter("roleid")));
	        }
	        if(request.getParameter("startdate")!=null&&!"".equals(request.getParameter("startdate"))){
	        	t_user_role.setStartdate(request.getParameter("startdate"));
	        }
	        if(request.getParameter("enddate")!=null&&!"".equals(request.getParameter("enddate"))){
	        	t_user_role.setEnddate(request.getParameter("enddate"));
	        }
	        t_user_role.setStartNo( startIndex );
	        t_user_role.setEndNo( startIndex + pageSize );
	    	List<T_user_role> findAll = service.findAll(t_user_role);
	        int totalCount = service.findCountAll(t_user_role);
	       	pg = new Pg(findAll, totalCount, pageSize, startIndex);
	       	request.setAttribute("t_user_roleList", findAll);
		    request.setAttribute("pg", pg);
	    } catch (Exception e) {
			e.printStackTrace();
			log.error("------query t_user_roleList error:"+e);
		}

		return pg;
	}

	/**
	 * 2、新增保存  T_user_role信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveT_user_roles")
	@ResponseBody
	public JsonVO saveT_user_roles(T_user_role t_user_role, HttpServletRequest request,HttpServletResponse response){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		String[] ids  = request.getParameterValues("ids");
		String userid = request.getParameter("userid");
		String uid = request.getParameter("id");

		//1、删除原有userid对应roleid数据；
		T_user_role obj = new T_user_role();
		obj.setUserid(userid);
		service.delete(obj);

		//2、插入新的数据
        vo.setResult("success");
        vo.setJieguo(true);
        T_user_role nr = new T_user_role();
		nr.setUserid(userid);
		if(ids!=null&&ids.length!=0){
			log.debug("------ids.length="+ids.length+";userid="+userid+";id="+uid);
			for(int i=0;i<ids.length;i++){
				nr.setRoleid(new Integer(ids[i]));
				if(service.save(nr)<=0){
			        vo.setResult("error");
			        vo.setJieguo(false);
				}
			}
		}

		return vo;
	}

	/**
	 * @deprecated 废弃的方法
	 * 2.、新增保存  T_user_role信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveT_user_role")
	@ResponseBody
	public JsonVO saveT_user_role(T_user_role t_user_role, HttpServletRequest request,HttpServletResponse response){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//如果有特殊需求，需要将json对象值转成java实体对象属性值
		if(service.save(t_user_role)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 3、修改T_user_role信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param t_user_role
	 * @param request
	 * @return JsonVO
	 */
	@RequestMapping("/updateT_user_role")
	@ResponseBody
	public JsonVO updateT_user_role(T_user_role t_user_role, HttpServletRequest request){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//如果有特殊值需要处理，在此处手工转换
		//T_user_role t_user_roleTemp = service.findById(t_user_role);
		//t_user_roleTemp.setXXX(t_user_role.getXXX());
		//if(service.update(t_user_roleTemp)){

		if(service.update(t_user_role)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 4、根据id查询T_user_role单个信息并显示在修改页面
	 * @param id
	 * @param request
	 * @return T_user_role json格式字符串
	 */
	@RequestMapping("/getT_user_role")
	@ResponseBody
	public T_user_role getT_user_role(Integer id,HttpServletRequest request){
		T_user_role t_user_role = new T_user_role();
		t_user_role.setId(id);
		return service.findById(t_user_role);
	}

	/**
	 * 5、删除T_user_role根据id
	 * @param id
	 * @param request
	 * @param response
	 * return JsonVO ：json格式字符串
	 */
	@RequestMapping("/delT_user_role")
	@ResponseBody
	public JsonVO delT_user_role(Integer id,HttpServletRequest request,HttpServletResponse response){
        JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);

		T_user_role t_user_role = new T_user_role();
		t_user_role.setId(id);

		if(service.delete(t_user_role)>0){
			vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 6、跳转到t_user_role/toT_user_roleList查询所有数据页面
	 *  return /WEB-INF/views/auth/t_user_role/t_user_roleList.jsp
	 */
    @RequestMapping(value = "/toT_user_roleList")
    public String toT_user_roleList() {
    	log.debug("------跳转到/WEB-INF/views/auth/t_user_role/t_user_roleList.jsp------");
        return "auth/t_user_role/t_user_roleList";
    }

	/**
	 * 7、新增、修改、详情页面t_user_role/toT_user_roleCurd跳转
     * auth/t_user_role/T_user_roleCurd.jsp
	 */
    @RequestMapping(value = "/toT_user_roleCurd")
    public String toT_user_roleCurd() {
    	log.debug("------跳转到/WEB-INF/views/auth/t_user_role/t_user_roleCurd.jsp------");
        return "auth/t_user_role/t_user_roleCurd";
    }

    /**
	 * 8、批量删除T_user_role根据主键id
	 * 是否要实现批量删除，待考虑，基本上用途不大
	 * @param id
	 * @param request
	 * @param response
	@RequestMapping("/delT_user_roleBatch")
	public String delT_user_roleBatch(HttpServletRequest request,HttpServletResponse response){
		String[] ids = request.getParameterValues("ids");
		System.out.println("------ids="+ids);
		T_user_role t_user_role = new T_user_role();
		int j=0;
		for(int i =0;i< ids.length;i++){
			j++;
			log.error("------ids[i]="+ids[i]);
			t_user_role.setId(new Integer(ids[i]).intValue());
			service.delete(t_user_role);
		}
		if(j==ids.length){
			log.debug("------t_user_role批量删除成功！------");
		}else{
			log.error("------t_user_role批量删除部分成功！部分失败！------");
		}

		return "redirect:/t_user_role/getAllT_user_role";
	}
*/



}









